package com.qingke.pms.dao;

import java.io.InputStream;
import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import com.qingke.pms.pojo.Company;
import com.qingke.pms.pojo.Material;
import com.qingke.pms.pojo.Project;
import com.qingke.pms.pojo.ProjectLeader;
import com.qingke.pms.pojo.PurchaseOrderStatus;
import com.qingke.pms.pojo.PurchasePlan;
import com.qingke.pms.pojo.PurchasePlanStatus;
import com.qingke.pms.pojo.RequisitionOrderStatus;
import com.qingke.pms.pojo.User;
import com.qingke.pms.pojo.UserLogin;


public class ProjectManagementSystemDAO {
	private String resource = "mybatis_conf.xml";
	private InputStream is=null;
	private SqlSessionFactory sessionFactory = null;
	private SqlSession session = null;
	
	public ProjectManagementSystemDAO(){
		
	}
	
	{
		is=ProjectManagementSystemDAO.class.getClassLoader().getResourceAsStream(resource);
		sessionFactory = new SqlSessionFactoryBuilder().build(is);
		session = sessionFactory.openSession();
	}
	
	public boolean register(UserLogin userLogin,User user){
		int count=0;
		UserLoginMapper ulm=session.getMapper(UserLoginMapper.class);
		UserMapper um=session.getMapper(UserMapper.class);
		try{
			count+=ulm.insert(userLogin);
			user.setId(userLogin.getId());
			count+=um.insert(user);
			session.commit();
		}catch(Exception e){
			e.printStackTrace();
			session.rollback();
		}
		if(count==2)
			return true;
		return false;
	}
	
	public boolean isUsernameExists(String username){
		UserLoginMapper ulm=session.getMapper(UserLoginMapper.class);
		UserLogin userLogin=ulm.selectByUsername(username);
		if(userLogin==null)
			return false;
		return true;
	}
	
	public boolean isUserExistById(int id){
		UserMapper um=session.getMapper(UserMapper.class);
		User user=um.selectByPrimaryKey(id);
		if(user==null)
			return false;
		return true;
	}
	
	public User login(String username,String password){
		if(!isUsernameExists(username))
			return null;
		UserLoginMapper ulm=session.getMapper(UserLoginMapper.class);
		UserLogin userLogin=ulm.selectByUsername(username);
		if(!userLogin.getPassword().equals(password))
			return null;
		UserMapper um=session.getMapper(UserMapper.class);
		User user=um.selectByPrimaryKey(userLogin.getId());
		return user;
	}
	
	public User selectUserById(int id){
		UserMapper um=session.getMapper(UserMapper.class);
		return um.selectByPrimaryKey(id);
	}
	
	public UserLogin selectUserLoginById(int id){
		UserLoginMapper ulm=session.getMapper(UserLoginMapper.class);
		return ulm.selectByPrimaryKey(id);
	}

	public List<User> selectAllUsers(){
		UserMapper um=session.getMapper(UserMapper.class);
		return um.selectAll();
	}
	
	public List<User> selectUsersByLikeName(String name){
		UserMapper um=session.getMapper(UserMapper.class);
		return um.selectByLikeName(name);
	}
	
	public List<User> selectUsersByLikeEmail(String email){
		UserMapper um=session.getMapper(UserMapper.class);
		return um.selectByLikeEmail(email);
	}
	
	public List<User> selectUsersByLikePhone(String phone){
		UserMapper um=session.getMapper(UserMapper.class);
		return um.selectByLikePhone(phone);
	}
	
	
	public boolean isMaterialExistById(int id){
		MaterialMapper mm=session.getMapper(MaterialMapper.class);
		Material material=mm.selectByPrimaryKey(id);
		if(material==null)
			return false;
		return true;
	}
	
	public boolean isMaterialExistByCode(String code){
		MaterialMapper mm=session.getMapper(MaterialMapper.class);
		Material material=mm.selectByCode(code);
		if(material==null)
			return false;
		return true;
	}
	
	public Material selectMaterialById(int id){
		MaterialMapper mm=session.getMapper(MaterialMapper.class);
		return mm.selectByPrimaryKey(id);
	}
	
	public Material selectMaterialByCode(String code){
		MaterialMapper mm=session.getMapper(MaterialMapper.class);
		return mm.selectByCode(code);
	}

	public List<Material> selectAllMaterials(){
		MaterialMapper mm=session.getMapper(MaterialMapper.class);
		return mm.selectAll();
	}
	
	public List<Material> selectMaterialsByLikeCode(String code){
		MaterialMapper mm=session.getMapper(MaterialMapper.class);
		return mm.selectByLikeCode(code);
	}
	
	public List<Material> selectMaterialsByLikeName(String name){
		MaterialMapper mm=session.getMapper(MaterialMapper.class);
		return mm.selectByLikeName(name);
	}
	
	public Project selectProjectById(int id){
		ProjectMapper pm=session.getMapper(ProjectMapper.class);
		return pm.selectByPrimaryKey(id);
	}
	
	public Project selectProjectByCode(String code){
		ProjectMapper pm=session.getMapper(ProjectMapper.class);
		return pm.selectByCode(code);
	}
	
	public List<Project> selectAllProjects(){
		ProjectMapper pm=session.getMapper(ProjectMapper.class);
		return pm.selectAll();
	}
	
	public List<Project> selectProjectsByLikeCode(String code){
		ProjectMapper pm=session.getMapper(ProjectMapper.class);
		return pm.selectByLikeCode(code);
	}
	
	public List<Project> selectProjectsByLikeName(String name){
		ProjectMapper pm=session.getMapper(ProjectMapper.class);
		return pm.selectByLikeName(name);
	}
	
	public List<Project> selectProjectsByConstructionUnitId(int id){
		ProjectMapper pm=session.getMapper(ProjectMapper.class);
		return pm.selectByConstructionUnitId(id);
	}
	
	public List<Project> selectProjectsByFirstPartyId(int id){
		ProjectMapper pm=session.getMapper(ProjectMapper.class);
		return pm.selectByFirstPartyId(id);
	}
	
	public boolean insertMaterial(Material material){
		MaterialMapper mm=session.getMapper(MaterialMapper.class);
		int count=mm.insert(material);
		session.commit();
		if(count==0)
			return false;
		return true;
	}
	
	public boolean updateMaterial(Material material){
		MaterialMapper mm=session.getMapper(MaterialMapper.class);
		int count=mm.updateByPrimaryKey(material);
		session.commit();
		if(count==0)
			return false;
		return true;
	}
	
	public boolean updateProject(Project project){
		ProjectMapper pm=session.getMapper(ProjectMapper.class);
		int count=pm.updateByPrimaryKey(project);
		session.commit();
		if(count==0)
			return false;
		return true;
	}
	
	public boolean updateUserLogin(UserLogin userLogin){
		UserLoginMapper ulm=session.getMapper(UserLoginMapper.class);
		int count=ulm.updateByPrimaryKey(userLogin);
		session.commit();
		if(count==0)
			return false;
		return true;
	}
	
	public boolean updateUser(User user){
		UserMapper um=session.getMapper(UserMapper.class);
		int count=um.updateByPrimaryKey(user);
		session.commit();
		if(count==0)
			return false;
		return true;
	}
	
	public boolean isCompanyExistById(int id){
		CompanyMapper mm=session.getMapper(CompanyMapper.class);
		Company company=mm.selectByPrimaryKey(id);
		if(company==null)
			return false;
		return true;
	}
	
	public boolean isCompanyExistByCode(String code){
		CompanyMapper mm=session.getMapper(CompanyMapper.class);
		Company company=mm.selectByCode(code);
		if(company==null)
			return false;
		return true;
	}
	
	public boolean insertCompany(Company company){
		CompanyMapper mm=session.getMapper(CompanyMapper.class);
		int count=mm.insert(company);
		session.commit();
		if(count==0)
			return false;
		return true;
	}
	
	public boolean isProjectExistById(int id){
		ProjectMapper pm=session.getMapper(ProjectMapper.class);
		Project project=pm.selectByPrimaryKey(id);
		if(project==null)
			return false;
		return true;
	}
	
	public boolean isProjectExistByCode(String code){
		ProjectMapper pm=session.getMapper(ProjectMapper.class);
		Project project=pm.selectByCode(code);
		if(project==null)
			return false;
		return true;
	}
	
	public boolean insertProject(Project company){
		ProjectMapper pm=session.getMapper(ProjectMapper.class);
		int count=pm.insert(company);
		session.commit();
		if(count==0)
			return false;
		return true;
	}
	
	public boolean isAdminstrator(User user){
		UserLoginMapper ulm=session.getMapper(UserLoginMapper.class);
		UserLogin userLogin=ulm.selectByPrimaryKey(user.getId());
		return userLogin.getIsAdministrator();
	}
	
	public boolean isLeader(Project project,User user){
		ProjectLeader projectLeader=new ProjectLeader();
		projectLeader.setProjectId(project.getId());
		projectLeader.setUserId(user.getId());
		return isProjectLeaderExistByProjectAndLeader(projectLeader);
	}
	
	public boolean isProjectLeaderExistByProjectAndLeader(ProjectLeader projectLeader){
		ProjectLeaderMapper plm=session.getMapper(ProjectLeaderMapper.class);
		ProjectLeader pl=plm.selectByProjectAndLeader(projectLeader);
		if(pl==null)
			return false;
		return true;
	}
	
	public boolean insertProjectLeader(ProjectLeader projectLeader){
		ProjectLeaderMapper plm=session.getMapper(ProjectLeaderMapper.class);
		int count=plm.insert(projectLeader);
		session.commit();
		if(count==0)
			return false;
		return true;
	}
	
	public List<ProjectLeader> selectProjectLeadersByProject(Project project){
		ProjectLeaderMapper plm=session.getMapper(ProjectLeaderMapper.class);
		return plm.selectByProjectId(project.getId());
	}
	
	public boolean isPurchasePlanExistByCode(String code){
		PurchasePlanMapper ppm=session.getMapper(PurchasePlanMapper.class);
		PurchasePlan pp=ppm.selectByCode(code);
		if(pp==null)
			return false;
		return true;
	}
	
	public PurchasePlan selectPurchasePlanByCode(String code){
		PurchasePlanMapper ppm=session.getMapper(PurchasePlanMapper.class);
		return ppm.selectByCode(code);
	}
	
	public PurchasePlan selectPurchasePlanById(int id){
		PurchasePlanMapper ppm=session.getMapper(PurchasePlanMapper.class);
		return ppm.selectByPrimaryKey(id);
	}
	
	public List<PurchasePlanStatus> selectAllPurchasePlanStatuses(){
		PurchasePlanStatusMapper ppsm=session.getMapper(PurchasePlanStatusMapper.class);
		return ppsm.selectAll();
	}
	
	public List<PurchaseOrderStatus> selectAllPurchaseOrderStatuses(){
		PurchaseOrderStatusMapper posm=session.getMapper(PurchaseOrderStatusMapper.class);
		return posm.selectAll();
	}
	
	public List<RequisitionOrderStatus> selectAllRequisitionOrderStatuses(){
		RequisitionOrderStatusMapper rosm=session.getMapper(RequisitionOrderStatusMapper.class);
		return rosm.selectAll();
	}
}
